package com.aliyun.tools.controldatarunnable;


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.gnome.gtk.Notebook;
import org.gnome.gtk.ScrolledWindow;
import org.gnome.gtk.TextBuffer;
import org.gnome.gtk.TextView;

import com.mine.control.ControlTable;
import com.mine.main.DiffOfReport;


public class createTableRunnable implements Runnable{
	
	String tableName;
	String inFilePath;
	String outFilePaht;
	String outFilePath;
	String version;
	String date;
	String category;
	
	String comparedTableName;
	
	Lock dblock;
	Notebook notebook;
	ScrolledWindow scrolledWindow;
	TextView textView;
	

	private boolean isActive = false;
	
	public createTableRunnable(Notebook notebook, int pageNum, String category, String date, String inFilePath, String version){
		this.notebook = notebook;
		this.scrolledWindow = (ScrolledWindow) notebook.getChildren()[pageNum];
		this.textView = (TextView) scrolledWindow.getChildren()[0];
		this.tableName = version;
		this.inFilePath = inFilePath;
		this.version = version;
		this.date = date;
		this.category = category;
		dblock = new ReentrantLock();
	}

	public void run() {
		while(getActive()){
			dblock.lock();
			try{
				Thread.yield();
				if(!ControlTable.searchTable(tableName))
					ControlTable.createTable(tableName);
				System.out.println("begin importing.......");
				ControlTable.inputTargerTable(inFilePath, tableName, category);		
				System.out.println("end import.....");
				System.out.println("begin comparing.....");
				TextBuffer textBuffer = textView.getBuffer();
				textBuffer.setText("读入完毕, 正在比对数据....");
				
				if (("Harmony").equals(category)) {
					//
					outFilePath = "harmony_diff"+Thread.currentThread().getName()+".txt";
					comparedTableName = "";
				}else if(("JCK_API").equals(category)) {
					outFilePath = "jckapi_diff"+Thread.currentThread().getName()+".txt";
					comparedTableName="dalvik_jck_api_report";
				} else if (("JCK_LANG").equals(category)) {
					outFilePath = "jcklang_diff"+Thread.currentThread().getName()+".txt";
					comparedTableName="dalvik_jck_lang_report";
				} else if (("JNI").equals(category)) {
					//
					outFilePath = "jni_diff"+Thread.currentThread().getName()+".txt";
					comparedTableName="";
				} else if(("STRESS").equals(category)){
					outFilePath = "stress_diff"+Thread.currentThread().getName()+".txt";
					comparedTableName="dalvik_stress_report";
				}
				DiffOfReport.diffReport(inFilePath, comparedTableName, outFilePath, category);
				FileReader fileReader = new FileReader(outFilePath);
				BufferedReader bufferReader = new BufferedReader(fileReader);
				String line = null;
				String buffer = "";
				while((line = bufferReader.readLine())!= null){
					buffer+=line+"\n";
				}
				textBuffer.setText("比对完毕，结果如下：\n");
				textBuffer.insert(textBuffer.getIterEnd(), buffer);
				setActive(false);
				System.out.println("end compare.....");
					
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				dblock.unlock();
			}
		}
		
	}
	
	public void setActive(boolean active){
		this.isActive = active;
	}
	
	public boolean getActive(){
		return this.isActive;
	}
	
}
